import json
from pyecharts.charts import Map
from pyecharts.options import *
import sys
sys.path.append("D:/Code/Python_code/python_learning/py_stage_1")
from my_package import province_rename as pr


address = "D:/Code/KnowCode/my_python/可视化案例数据/地图数据/疫情.txt"
f = open(address, 'r', encoding="UTF-8")
data = f.read()
f.close()

# json转换字典
dic = json.loads(data)
# print(data)

# 取得省份数据
provinces = dic['areaTree'][0]["children"]

# 组装成map地图数据(列表嵌套元组)
data_list = []
for each in provinces:
    name = each["name"]
    name = pr.trans(name)
    confirm = each["total"]["confirm"]
    data_list.append((name, confirm))

print(data_list)

# 构建地图
mmap = Map()

mmap.add("各省份确诊人数", data_list, "china")

mmap.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min": 1, "max": 99, "label": "1~99人", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "label": "100~999人", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "label": "1000~4999人", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "label": "5000~9999人", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "label": "10000~99999人", "color": "#CC3333"},
            {"min": 100000, "label": "100000人以上", "color": "#990033"}
        ]
    )
)

mmap.render("全国疫情地图.html")
